package com.chancingpack.controller;

import com.alibaba.fastjson.JSONObject;
import com.chancingpack.dal.model.WxUserLogin;
import com.chancingpack.service.WxUserLoginService;
import com.chancingpack.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;

/**
 * Created by Administrator on 2017/7/13.
 */
@Controller
public class IndexController {
    @Autowired
    private WxUserLoginService wxUserLoginService;

    //后台登陆页面
    @RequestMapping("/login")
    public ModelAndView login(){
        ModelAndView view = new ModelAndView("login");
        return view;
    }

    //后台系统登录验证
    @RequestMapping("/checkLogin")
    @ResponseBody
    public String checkLogin(@RequestBody WxUserLogin wxUserLogin,HttpServletRequest request){
        CommonResult result = this.wxUserLoginService.getCheckLogin(wxUserLogin);
        if( wxUserLogin!=null) {
            String username=wxUserLogin.getUsername();
            WxUserLogin re = this.wxUserLoginService.getListByName(username);
            if (null!=re){
            String custId = re.getCustId();
            request.getSession().setAttribute("custId", custId);
            int roleId = re.getRoleId();
            request.getSession().setAttribute("roleId", roleId);
            }
        }
        return JSONObject.toJSONString(result);
    }

    //后台系统登录验证成功跳转
    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView view = new ModelAndView("index");
        return view;
    }

    //微信登陆页面
    @RequestMapping("/wxLogin")
    public ModelAndView wxLogin(){
        ModelAndView view = new ModelAndView("/wxlogin/wxlogin");
        return view;
    }

    //微信用户登录验证
    @RequestMapping("/wxCheckLogin")
    @ResponseBody
    public CommonResult wxCheckLogin(@RequestBody WxUserLogin wxUserLogin, HttpServletRequest request){
        CommonResult result = this.wxUserLoginService.getWxCheckLogin(wxUserLogin);

        if(result.getIsSuccess()){
            WxUserLogin wxUserLogin1=this.wxUserLoginService.getListByName(wxUserLogin.getUsername());
            request.getSession().setAttribute("custId", wxUserLogin1.getCustId());
            String cust_name=wxUserLoginService.getSgbasCustInfoByCustId(wxUserLogin1.getCustId()).get(0).get("cust_name").toString();
            request.getSession().setAttribute("cust_name",cust_name);
//          httpSession.setAttribute("custId", wxUserLogin1.getCustId());
        }
        return result;
    }

    //后台系统登录验证成功跳转
//    @RequestMapping("/myproduct/myProduct")
//    public ModelAndView myProduct(String username,HttpSession httpSession){
//        ModelAndView view = new ModelAndView("myproduct/myProduct");
//        WxUserLogin wxUserLogin=this.wxUserLoginService.getListByName(username);
//        String custId=wxUserLogin.getCustId();
//        System.out.println(custId+"-------------------------------------");
//
//
//        return view;
//    }




    @RequestMapping("/pro")
    public ModelAndView pro(HttpSession httpSession){
        ModelAndView view = new ModelAndView("myproduct/showProduct");

        return view;
    }


}
